/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sharefileclient;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author anwedo
 */
public class ListenRequestThread extends Thread {

    final public static int BUFFER_SIZE = 2048;
    final public static int PACKET_SIZE = 2048;
    final public static long PART_SIZE = 1000000;

    public ClientGUI mainThread;

    public ListenRequestThread(ClientGUI mainThread) {
        this.mainThread = mainThread;
    }

    @Override
    public void run() {
        System.out.println("Start listening...");
        String ip = "localhost";
        try {
            InetAddress addr = InetAddress.getLocalHost();
            ip = addr.getHostAddress();
        } catch (UnknownHostException ex) {
            Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            //crate registry port to client can connect
            RMIClient serverOnClient = new RMIClient(this);
            LocateRegistry.createRegistry(2013);
            String url = "rmi://" + ip + ":2013/ShareServerOnClient";
            //binding server handler with url
            Naming.rebind(url, serverOnClient);
            System.out.println("ServerOnClient is running.....");
        } catch (MalformedURLException | RemoteException ex) {
            Logger.getLogger(RMIClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
